package jwt

import (
	"gitee.com/xluomo945/fm/setting"
	"github.com/gin-gonic/gin"
	"time"
)

var config = defaultConfig

var defaultConfig = &jwtConfig{
	Token:    func(ctx *gin.Context) string { return "" },
	Code:     func() int { return -1 },
	HttpCode: func() int { return 503 },
	Message:  func(err error) string { return "config invalid" },
}

type jwtConfig struct {
	SignKey  string
	Expire   time.Duration
	Token    func(ctx *gin.Context) string
	Code     func() int
	HttpCode func() int
	Message  func(error) string
}

func SetConfig(cfg setting.JwtConfig) {
	if cfg != nil {
		config = &jwtConfig{
			SignKey:  cfg.GetSignKey(),
			Expire:   cfg.GetExpireTime(),
			Token:    cfg.GetToken,
			Code:     cfg.UnAuthorizedCode,
			HttpCode: cfg.UnAuthorizedHttpCode,
			Message:  cfg.UnAuthorizedMessage,
		}
	}
}
